package com.car.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.account.controller.form.storesInfoForm.AdminSelectStoresInfoForm;
import com.car.account.dto.storesInfoDto.AdminSelectStoresInfoByIdDto;
import com.car.account.dto.storesInfoDto.AdminSelectStoresInfoDto;
import com.car.account.dto.storesInfoDto.StoresInfoDto;
import com.car.account.entity.StoresInfo;
import com.car.account.exception.AccountException;
import com.car.account.param.storesInfoParam.AdminSelectStoresInfoParam;
import com.car.account.service.StoresInfoService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/storesInfo")
@Api(tags = "门店信息")
public class StoresInfoController {

    @Resource
    private StoresInfoService storesInfoService;

    @GetMapping("/adminSelectStoresInfoDtoById.pt")
    @ApiOperation(value = "超管根据门店id查询门店信息")
    public Result<AdminSelectStoresInfoByIdDto> adminSelectStoresInfoDtoById(Integer storesId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        AdminSelectStoresInfoByIdDto dto = storesInfoService.adminSelectStoresInfoDtoById(adminId, storesId);
        return Result.getResult(AdminSelectStoresInfoByIdDto.class).setData(dto).setMsg("超管根据id查询门店信息成功");
    }

    @PostMapping("/adminSelectStoresInfoDto.pt")
    @ApiOperation(value = "超管根据门店名称模糊查询门店信息")
    public Result<Page> adminSelectStoresInfoDto(@RequestBody AdminSelectStoresInfoForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        AdminSelectStoresInfoParam param = BeanUtil.toBean(form, AdminSelectStoresInfoParam.class);
        param.setAdminId(adminId);
        Page<AdminSelectStoresInfoDto> page = storesInfoService.adminSelectStoresInfoDto(param);
        return Result.getResult(Page.class).setData(page).setMsg("超管查询门店信息成功");
    }

    @GetMapping("/adminDisableStores.pt")
    @ApiOperation(value = "超管禁用门店")
    public Result adminDisableStores(Integer storesId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        storesInfoService.adminDisableStores(adminId, storesId);
        return Result.getResult().setMsg("超管禁用门店成功");
    }

    @GetMapping("/adminEnableStores.pt")
    @ApiOperation(value = "超管启用门店")
    public Result adminEnableStores(Integer storesId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        storesInfoService.adminEnableStores(adminId, storesId);
        return Result.getResult().setMsg("超管启用门店成功");
    }

    @GetMapping("/clerkSelectStoresInfo.md")
    @ApiOperation(value = "店长查询门店信息")
    public Result<StoresInfo> clerkSelectStoresInfo() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        StoresInfo storesInfo = storesInfoService.clerkSelectStoresInfo(clerkId);
        return Result.getResult(StoresInfo.class).setData(storesInfo).setMsg("店长查询门店信息");
    }

    @PostMapping("/updateStoresInfoImage.md")
    @ApiOperation(value = "店长修改门店封面")
    public Result<String> updateStoresInfoImage(MultipartFile mf) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        String imageName = storesInfoService.updateStoresInfoImage(clerkId, mf);
        return Result.getResult(String.class).setData(imageName).setMsg("店长修改门店封面成功");
    }

    @GetMapping("/updateStoresInfoDetail.md")
    @ApiOperation(value = "店长修改门店详情")
    public Result updateStoresInfoDetail(String storesDetail) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        storesInfoService.updateStoresInfoDetail(clerkId, storesDetail);
        return Result.getResult().setMsg("店长修改门店详情成功");
    }

    /**
     * openfeign
     * @return 返回所有状态正常的门店的id
     * @throws Exception
     */
    @GetMapping("/getAllStoresId.ty")
    @ApiOperation(value = "查询所有正常状态门店id")
    Result<List<Integer>> getAllStoresId() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        List<Integer> storesInfoIds = storesInfoService.getAllStoresId(adminId);
        return Result.getListResult(Integer.class).setData(storesInfoIds).setMsg("查询所有正常状态门店id成功");
    }

    /**
     * 通过门店id返回一个门店dto
     * @param storesId
     * @return
     * @throws Exception
     */
    @GetMapping("/findStoresById/{storesId}.ty")
    public Result<StoresInfoDto> findStoresById(@PathVariable("storesId") Integer storesId) throws Exception{
        StoresInfo storesInfo = storesInfoService.getById(storesId);
        if (storesInfo == null) throw new AccountException(3001,"不存在门店信息");
        return Result.getResult(StoresInfoDto.class)
                .setData(BeanUtil.toBean(storesInfo, StoresInfoDto.class))
                .setMsg("通过门店id返回一个门店dto成功");
    }

    /**
     * openfeign
     */
    @GetMapping("/storesInfo/changeStoresInfoScore.ty")
    public Result changeStoresInfoScore(@RequestParam("score") Integer score,@RequestParam("storesId") Integer storesId) throws Exception{
        storesInfoService.changeStoresInfoScore(score, storesId);
        return Result.getResult().setMsg("修改门店评分成功");
    }

}

